﻿app.controller('ChurchWorkerInfoController', ['$scope', 'GlobalService', 'fileUpload', 'ApplicationService', 'alertboxservice', 'GlobalFactory', function ($scope, GlobalService, fileUpload, ApplicationService, alertboxservice, GlobalFactory) {

    $scope.Genders = GlobalService.GetGenders();

    GlobalService.GetNameTitle().success(function (responce) { $scope.NameTitles = responce });

    GlobalService.GetChurchWorkerDesignation().success(function (responce) { $scope.ChurchWorkerDesignation = responce });

    GlobalService.GetCountry().success(function (data) { $scope.countries = data; });

    GlobalService.GetChurchWorkerStatus().success(function (data) { $scope.ChurchWorkerStatuss = data; });

    $scope.$watch('ChurchWorker.PresentAddress.PresentCountryId', function (newVal) {
        if (angular.isUndefined(newVal) == false) {
            GlobalService.GetStates(newVal).success(function (data) { $scope.states = data; });
        }
    });

    BindChurchWorkerInfo();

    function BindChurchWorkerInfo() {
        url = "GetChurchWorker";
        var id = getQueryString('ChurchWorkerId');
        
        ApplicationService.Getdata(url, id).success(function (data) {
            $scope.entity = data;
            $scope.ChurchWorker = data;
            console.log($scope.ChurchWorker);
        });
    }

    $scope.edit = function (id) {
       
        $("#ChurchWorker").modal({ backdrop: 'static' });
    }

    $scope.fileChangedChurchWorkerImage = function (evt) {
        var file = evt.currentTarget.files[0];
        var reader = new FileReader();
        reader.onload = function (evt) {
            $scope.$apply(function ($scope) {
                $scope.ChurchWorker.Photo = evt.target.result;
            });
        };
        reader.readAsDataURL(file);
    }

    $scope.fileChangedChurchWorkerApplication = function (evt) {
        var file = evt.currentTarget.files[0];
        var reader = new FileReader();
        reader.onload = function (evt) {
            $scope.$apply(function ($scope) {
                $scope.ChurchWorker.ApplicationCopy = evt.target.result;
            });
        };
        reader.readAsDataURL(file);
    }

    $scope.save = function (id) {
        $scope.buttonstate = true;
        url = "UpdateChurchWorker";
        ApplicationService.Save($scope.ChurchWorker, url).success(function (result) {
            alertboxservice.ShowMsg(result, "ChurchWorker ");
            $scope.buttonstate = false;
            BindChurchWorkerInfo();
        });
    }
}]);